//
//  ReadListInfoViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 Hero. All rights reserved.
//

#import "ReadListInfoViewController.h"

@interface ReadListInfoViewController ()<UIWebViewDelegate>
@property (nonatomic,strong)UIWebView *webView;
@property (nonatomic,strong)NSString *html;
@property (nonatomic,assign)BOOL isloading;
@end

@implementation ReadListInfoViewController
- (UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:self.view.frame];
        
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;

    }
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.webView];
    
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction)];
//    
   
    [RequestManager fetchDataWithUrl:kReadListInfoUrl parameter:@{@"contentid":self.readListModel.ID} mehtod:@"POST" compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",bigDic[@"data"][@"html"]);
        //根据<pre>进行分割
        NSArray *array = [bigDic[@"data"][@"html"]componentsSeparatedByString:@"<pre>"];
        
        [self.webView loadHTMLString:array[0] baseURL:nil];
        self.html = array[0];
        
    } updata:^{
        
    }];
    
    
}
- (void)itemAction{
   
        [[CoreDataManager defaultManager]insertCollectionModelWithReadModel:self.readListModel html:self.html];
    
}



- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"开始加载");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction)];

  
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
